package com.hhl.flower.controller;

import com.hhl.flower.common.result.Result;
import com.hhl.flower.context.BaseContext;
import com.hhl.flower.pojo.entity.Address;
import com.hhl.flower.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/address")
@Api(tags = "用户端地址相关接口")
@Slf4j
public class AddressController {
	@Autowired
	private AddressService addressService;

	@GetMapping("/list")
	@ApiOperation("获取用户地址列表")
	public Result<List<Address>> list() {
		Integer userId = BaseContext.getCurrentId();

		List<Address> addressByUser = addressService.getAddressByUser(userId);

		return Result.success(addressByUser);
	}

	@PostMapping("/add")
	@ApiOperation("添加地址")
	public Result add(@RequestParam("consignee") String consignee, @RequestParam("phone") String phone, @RequestParam("detail") String detail, @RequestParam("isDefault") Integer isDefault) {

		Integer userId = BaseContext.getCurrentId();
		addressService.addAddress(userId, consignee, phone, detail, isDefault);
		return Result.success();
	}

	@PostMapping("/delete")
	@ApiOperation("删除地址")
	public Result delete(@RequestParam("id") Integer id) {

		Integer userId = BaseContext.getCurrentId();
		addressService.deleteAddress(userId, id);
		return Result.success();
	}

	@GetMapping("/getById")
	@ApiOperation("根据Id查询地址信息")
	public Result<Address> getById(@RequestParam("aid") Integer aid) {

		return Result.success(addressService.getById(aid));
	}




}
